/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.Processcard;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.ProcesscardRepository;
import me.zhengjie.service.ProcesscardService;
import me.zhengjie.service.dto.ProcesscardDto;
import me.zhengjie.service.dto.ProcesscardQueryCriteria;
import me.zhengjie.service.mapper.ProcesscardMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author zhangYY
* @date 2024-01-12
**/
@Service
@RequiredArgsConstructor
public class ProcesscardServiceImpl implements ProcesscardService {

    private final ProcesscardRepository processcardRepository;
    private final ProcesscardMapper processcardMapper;

    @Override
    public PageResult<ProcesscardDto> queryAll(ProcesscardQueryCriteria criteria, Pageable pageable){
        Page<Processcard> page = processcardRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(processcardMapper::toDto));
    }

    @Override
    public List<ProcesscardDto> queryAll(ProcesscardQueryCriteria criteria){
        return processcardMapper.toDto(processcardRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProcesscardDto findById(Integer id) {
        Processcard processcard = processcardRepository.findById(id).orElseGet(Processcard::new);
        ValidationUtil.isNull(processcard.getId(),"Processcard","id",id);
        return processcardMapper.toDto(processcard);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Processcard resources) {
        if(processcardRepository.findByProcesscardCode(resources.getProcesscardCode()) != null){
            throw new EntityExistException(Processcard.class,"processcard_code",resources.getProcesscardCode());
        }
        processcardRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Processcard resources) {
        Processcard processcard = processcardRepository.findById(resources.getId()).orElseGet(Processcard::new);
        ValidationUtil.isNull( processcard.getId(),"Processcard","id",resources.getId());
        Processcard processcard1 = null;
        processcard1 = processcardRepository.findByProcesscardCode(resources.getProcesscardCode());
        if(processcard1 != null && !processcard1.getId().equals(processcard.getId())){
            throw new EntityExistException(Processcard.class,"processcard_code",resources.getProcesscardCode());
        }
        processcard.copy(resources);
        processcardRepository.save(processcard);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            processcardRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProcesscardDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProcesscardDto processcard : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("工艺卡编码", processcard.getProcesscardCode());
            map.put("工程号", processcard.getWorkNum());
            map.put("产品类型", processcard.getProductType());
            map.put("材质", processcard.getMaterial());
            map.put("厚度", processcard.getThick());
            map.put("板子尺寸1", processcard.getSize1());
            map.put("板子尺寸2", processcard.getSize2());
            map.put("压合层名", processcard.getLayerName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}